package org.geek4j.base.interceptor;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Geek4J
 * @since 2016年4月9日 上午10:57:19
 * @description Log runtime exception
 */
@Slf4j
public class LogInterceptor extends HandlerInterceptorAdapter {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		if (ex != null) {
			StringBuffer parameter = new StringBuffer();
			Map<String, String[]> parameterMap = request.getParameterMap();
			if (parameterMap != null) {
				for (Entry<String, String[]> entry : parameterMap.entrySet()) {
					String parameterName = entry.getKey();
					if (!"password".equals(parameterName)) {
						String[] parameterValues = entry.getValue();
						if (parameterValues != null) {
							for (String parameterValue : parameterValues) {
								parameter.append(parameterName + " = " + parameterValue + "\n");
							}
						}
					}
				}
			}
			log.error(parameter.toString(), ex);
		}
	}

}